package io.modifier.basic.mvi

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

abstract class BaseVM : ViewModel() {

    suspend fun <T> switch2UI(block: suspend CoroutineScope.() -> T) {
        withContext(Dispatchers.Main) { block() }
    }

    fun launchOnUI(block: suspend CoroutineScope.() -> Unit) {
        viewModelScope.launch(Dispatchers.Main) { block() }
    }

    suspend fun <T> switch2IO(block: suspend CoroutineScope.() -> T) {
        withContext(Dispatchers.IO) { block() }
    }

    suspend fun <T> launchOnIO(block: suspend CoroutineScope.() -> T) {
        viewModelScope.launch(Dispatchers.IO) { block() }
    }

}
